package robuin.ioio.config;

//import robuin.ioio.config.IOIOSeek.SlowAdapter;


import java.util.ArrayList;
import java.util.List;

import robuin.ioio.config.R;
import ioio.lib.api.DigitalInput;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.android.IOIOActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;

	public class digitalInput extends IOIOActivity{
		List<TextView> listTextViews_ = new ArrayList<TextView>();
	    private TextView mStatus;

	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);

	        Intent intent = getIntent();
	        String message = intent.getStringExtra(IOIOConfig.EXTRA_MESSAGE);
	        
	        setContentView(R.layout.digitalinput);

	        listTextViews_.add((TextView)findViewById(R.id.TextView01));
	        listTextViews_.add((TextView)findViewById(R.id.TextView02));
	        listTextViews_.add((TextView)findViewById(R.id.TextView03));
	        listTextViews_.add((TextView)findViewById(R.id.TextView04));
	        listTextViews_.add((TextView)findViewById(R.id.TextView05));
	        listTextViews_.add((TextView)findViewById(R.id.TextView06));
	        listTextViews_.add((TextView)findViewById(R.id.TextView07));
	        listTextViews_.add((TextView)findViewById(R.id.TextView08));
	        listTextViews_.add((TextView)findViewById(R.id.TextView09));
	        listTextViews_.add((TextView)findViewById(R.id.TextView10));
	        listTextViews_.add((TextView)findViewById(R.id.TextView11));
	        listTextViews_.add((TextView)findViewById(R.id.TextView12));
	        listTextViews_.add((TextView)findViewById(R.id.TextView13));
	        listTextViews_.add((TextView)findViewById(R.id.TextView14));
	        listTextViews_.add((TextView)findViewById(R.id.TextView15));
	        listTextViews_.add((TextView)findViewById(R.id.TextView16));
	        listTextViews_.add((TextView)findViewById(R.id.TextView17));
	        listTextViews_.add((TextView)findViewById(R.id.TextView18));
	        listTextViews_.add((TextView)findViewById(R.id.TextView19));
	        listTextViews_.add((TextView)findViewById(R.id.TextView20));
	        listTextViews_.add((TextView)findViewById(R.id.TextView21));
	        listTextViews_.add((TextView)findViewById(R.id.TextView22));
	        listTextViews_.add((TextView)findViewById(R.id.TextView23));
	        listTextViews_.add((TextView)findViewById(R.id.TextView24));
	        listTextViews_.add((TextView)findViewById(R.id.TextView25));
	        listTextViews_.add((TextView)findViewById(R.id.TextView26));
	        listTextViews_.add((TextView)findViewById(R.id.TextView27));
	        listTextViews_.add((TextView)findViewById(R.id.TextView28));
	        listTextViews_.add((TextView)findViewById(R.id.TextView29));
	        listTextViews_.add((TextView)findViewById(R.id.TextView30));
	        listTextViews_.add((TextView)findViewById(R.id.TextView31));
	        listTextViews_.add((TextView)findViewById(R.id.TextView32));
	        listTextViews_.add((TextView)findViewById(R.id.TextView33));
	        listTextViews_.add((TextView)findViewById(R.id.TextView34));
	        listTextViews_.add((TextView)findViewById(R.id.TextView35));
	        listTextViews_.add((TextView)findViewById(R.id.TextView36));
	        listTextViews_.add((TextView)findViewById(R.id.TextView37));
	        listTextViews_.add((TextView)findViewById(R.id.TextView38));
	        listTextViews_.add((TextView)findViewById(R.id.TextView39));
	        listTextViews_.add((TextView)findViewById(R.id.TextView40));
	        listTextViews_.add((TextView)findViewById(R.id.TextView41));
	        listTextViews_.add((TextView)findViewById(R.id.TextView42));
	        listTextViews_.add((TextView)findViewById(R.id.TextView43));
	        listTextViews_.add((TextView)findViewById(R.id.TextView44));
	        listTextViews_.add((TextView)findViewById(R.id.TextView45));
	        listTextViews_.add((TextView)findViewById(R.id.TextView46));
	        listTextViews_.add((TextView)findViewById(R.id.TextView47));
	        listTextViews_.add((TextView)findViewById(R.id.TextView48));
	        
	        mStatus = (TextView) findViewById(R.id.status1);
	        mStatus.setText(message);
	        enableUi(false);
	    }
	    
	    class IOIOThread extends BaseIOIOLooper {
	    	private List<DigitalInput> listInputs_ = new ArrayList<DigitalInput>();
			
	    	@Override
			public void setup() throws ConnectionLostException {
				try {
					for(int i = 1; i <= 48; i++)
						listInputs_.add(ioio_.openDigitalInput(i, DigitalInput.Spec.Mode.PULL_DOWN));
					enableUi(true);
				} catch (ConnectionLostException e) {
					enableUi(false);
					throw e;
				}
			}
			
			@Override
			public void loop() throws ConnectionLostException {
				try {
					for(int i = 0; i < listInputs_.size(); i++) 
						setText(listTextViews_.get(i), listInputs_.get(i).read());
					Thread.sleep(100);
				} catch (InterruptedException e) {
					ioio_.disconnect();
				} catch (ConnectionLostException e) {
					enableUi(false);
					throw e;
				}
			}
			
			@Override
	    	public void disconnected(){
	    		for(int i = 0; i < listInputs_.size(); i++)
	    			listInputs_.get(i).close();
	    	}
		}
	    
	    /**
		 * A method to create our IOIO thread.
		 * 
		 * @see ioio.lib.util.AbstractIOIOActivity#createIOIOThread()
		 */
		@Override
		protected IOIOLooper createIOIOLooper() {
			return new IOIOThread();
		}
	    
		private void enableUi(final boolean enable) {
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					for(int i = 0; i < 48; i++)
						listTextViews_.get(i).setEnabled(enable);
				}
			});
		}
		
		private void setText (final TextView textView, final boolean isHigh) {
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					if(isHigh)
						textView.setText("1");
					else
						textView.setText("0");	
				}
			});
		}
	}